package com.huiminpay.merchant.controller;

import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.merchant.common.SecurityUtil;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.service.IAppService;
import com.huiminpay.transaction.service.IPayChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "应用操作",tags = {"应用"},description = "应用CRUD操作")
@RestController
public class AppController {
    @Reference
    IAppService appService;

    @Reference
    IPayChannelService payChannelService;

    @PostMapping(value = "/my/apps")
    public AppDTO createApp(@RequestBody AppDTO appDTO){
        // 1.非空
        if(StringUtils.isEmpty(appDTO.getAppName())){
            throw new BusinessException(CommonErrorCode.E_200239);
        }

        // 2.商户ID
        //没有商户ID如何获取
        Long merchantId = SecurityUtil.getMerchantId();
        appDTO.setMerchantId(merchantId);//不要忘记set
        // 3.创建应用

        return appService.createApp(appDTO);
    }

    @ApiOperation("查询商户下的应用列表")
    @GetMapping(value = "/my/apps")
    public List<AppDTO> myApps(){
        //没有商户ID如何获取
        Long merchantId = SecurityUtil.getMerchantId();
        return appService.queryMerchantApp(merchantId);
    }

    @ApiOperation("根据appid获取应用的详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId", value = "商户应用id", required = true, dataType = "String", paramType = "path")})
    @GetMapping(value = "/my/apps/{appId}")
    public AppDTO myApp(@PathVariable String appId){

        return appService.myAppInfo(appId);
    }

    @ApiOperation("绑定服务类型")
    @PostMapping(value="/my/apps/{appId}/platform-channels")
    public void appBindPlateformChannel(@PathVariable String appId,@RequestParam String platformChannelCodes){

        payChannelService.bindPlatformChannelForApp(appId,platformChannelCodes);
    }

    @ApiOperation("查询应用是否绑定某平台服务")
    @GetMapping("/my/merchants/apps/platformchannels")
    public int queryAppBindPlatformChannel(@RequestParam String appId, @RequestParam String
            platformChannel){
        return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }
}
